[OMNIML-5613] Quantize ResNet residual adds in torch ONNX example - #2024
[OMNIML-5613] Quantize ResNet residual adds in torch ONNX example#2024ajrasane wants to merge 15 commits into
Conversation
Signed-off-by: ajrasane <131806219+ajrasane@users.noreply.github.com>
Signed-off-by: ajrasane <131806219+ajrasane@users.noreply.github.com>
|
Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually. Contributors can view more details about this message here. |
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
📝 WalkthroughWalkthroughThe PR adds timm ResNet quantization plugins and PTQ recipes, integrates recipe selection into the Torch ONNX example, extends AutoQuantize matching and cost handling, and updates ONNX FP8/INT8 FP16 transformations with structural validation. Changestimm ResNet quantization and export
Estimated code review effort: 4 (Complex) | ~60 minutes Suggested reviewers: 🚥 Pre-merge checks | ✅ 5 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (5 passed)
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
🧪 Generate unit tests (beta)
Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2024 +/- ##
==========================================
- Coverage 78.60% 76.82% -1.78%
==========================================
Files 522 523 +1
Lines 60167 62598 +2431
==========================================
+ Hits 47294 48094 +800
- Misses 12873 14504 +1631
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Signed-off-by: ajrasane <131806219+ajrasane@users.noreply.github.com>
cjluo-nv
left a comment
There was a problem hiding this comment.
Bot review (bedrock-claude-opus-5) — DM the bot to share feedback.
Small, focused change (+90/-2) that adds per-block residual-add quantizers to the timm ResNet path of the torch→ONNX example, with CHANGELOG + README updates and an ONNX-level assertion in the existing example test. No licensing concerns; no prompt-injection content in the PR metadata. A few things worth addressing before merge:
-
Duplicated calibration logic / extra data pass.
_add_resnet_residual_quantizersre-implements the enable_calib → forward-loop →load_calib_amaxdance that_calibrate_uncalibrated_quantizers(same file) already performs, and adds a second full pass over the calibration set for ResNet (the first happens insidequantize_modelfor the FP8 Conv overrides). If the residual quantizer were attached asinput_quantizeron the activation module and created beforequantize_model, both the calibration and the dead-quantizer guard would come for free from the existing helpers. Even more idiomatic: modelopt already supports this viaQuantModuleRegistry.register({nn.ReLU: "nn.ReLU"})(QuantInputBase)(exactly whatmodelopt/torch/quantization/nn/modules/quant_activations.pydoes fornn.LeakyReLU) plus a{"parent_class": "nn.ReLU", "quantizer_name": "*input_quantizer"}config entry, which gets calibrated bymtq.quantize's forward loop and is visible tomtq.print_quant_summary/ modelopt state. Please either reuse one of these paths or note in the PR/comment why the manual hook is needed. -
Residual quantizers bypass the file's own
amax<=0/NaN guard, andload_calib_amax()is strict._disable_dead_quantizersonly inspectsinput_quantizer/output_quantizer/weight_quantizer, and it runs insidequantize_model— i.e. before these quantizers exist. A residual quantizer that calibrates toamax == 0(or NaN) will therefore reach the FP8 exporter, which is precisely thescale = 448 / amaxdivision the guard exists to prevent. Also, unlike_calibrate_uncalibrated_quantizers, this code callsload_calib_amax()withoutstrict=False, so any block that didn't see data raises. -
automode picks the residual format from the search space, not the search result. Aftermtq.auto_quantize, each block's actual format is known (e.g.block.conv3.input_quantizer._num_bits); derivingnum_bitsfrom the union of requested formats can give an FP8 residual Q/DQ next to an INT8-quantized block (or the reverse), which is what the rest of this file goes to some length to avoid for TRT.
Minor: the two new functions are the only helpers in this file without docstrings; and assert len(residual_adds) == 16 asserts that every Add in the exported graph is a residual add, which will break confusingly if the exporter ever emits an unrelated Add — consider filtering to the 16 residual adds (e.g. by producer/consumer pattern) before the count assertion.
There was a problem hiding this comment.
Warning
CodeRabbit couldn't request changes on this pull request because it doesn't have sufficient GitHub permissions.
Please grant CodeRabbit Pull requests: Read and write permission and re-run the review.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@examples/torch_onnx/torch_quant_to_onnx.py`:
- Around line 223-239: Update _add_resnet_residual_quantizers and the
surrounding auto-quantization flow so residual quantizers are installed and
configured before mtq.auto_quantize() runs. For auto mode, include these
residual quantizer modules in every candidate format configuration used by the
search, ensuring their forced INT8/FP8 precision is scored and counted toward
the effective-bits constraint while preserving the existing non-auto behavior.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Enterprise
Run ID: 9cd948d5-f05e-4d18-9714-517039e16f2d
📒 Files selected for processing (4)
CHANGELOG.rstexamples/torch_onnx/README.mdexamples/torch_onnx/torch_quant_to_onnx.pytests/examples/torch_onnx/test_torch_quant_to_onnx.py
Signed-off-by: ajrasane <131806219+ajrasane@users.noreply.github.com>
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
examples/torch_onnx/torch_quant_to_onnx.py (1)
636-642:⚠️ Potential issue | 🟠 MajorInstall residual quantizers before the quantization/search pass.
At Line 636, residual quantizers are added only after
quantized_modelhas been created. Inautomode, they are therefore absent from candidate scoring and effective-bits constraints; the later heuristic can also choose a format different from the per-block format selected by AutoQuantize. The standard path additionally requires a second calibration pass and runs dead-quantizer cleanup before these modules exist.Move installation/configuration before quantization, or explicitly integrate these quantizers into AutoQuantize and rerun cleanup after calibration.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@examples/torch_onnx/torch_quant_to_onnx.py` around lines 636 - 642, Move the `_add_resnet_residual_quantizers` installation and configuration before the quantization/search pass creates `quantized_model`, so residual quantizers participate in AutoQuantize candidate scoring and effective-bits constraints. Ensure calibration and dead-quantizer cleanup operate on these modules, and remove the current post-quantization-only installation path.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In `@examples/torch_onnx/torch_quant_to_onnx.py`:
- Around line 636-642: Move the `_add_resnet_residual_quantizers` installation
and configuration before the quantization/search pass creates `quantized_model`,
so residual quantizers participate in AutoQuantize candidate scoring and
effective-bits constraints. Ensure calibration and dead-quantizer cleanup
operate on these modules, and remove the current post-quantization-only
installation path.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Enterprise
Run ID: 0ba0d581-0d68-4e4d-993c-47c5819b72dd
📒 Files selected for processing (4)
CHANGELOG.rstexamples/torch_onnx/README.mdexamples/torch_onnx/torch_quant_to_onnx.pytests/examples/torch_onnx/test_torch_quant_to_onnx.py
🚧 Files skipped from review as they are similar to previous changes (3)
- examples/torch_onnx/README.md
- CHANGELOG.rst
- tests/examples/torch_onnx/test_torch_quant_to_onnx.py
Co-Authored-By: Codex <noreply@openai.com> Signed-off-by: ajrasane <131806219+ajrasane@users.noreply.github.com>
|
There was a problem hiding this comment.
Warning
CodeRabbit couldn't request changes on this pull request because it doesn't have sufficient GitHub permissions.
Please grant CodeRabbit Pull requests: Read and write permission and re-run the review.
Actionable comments posted: 4
🧹 Nitpick comments (5)
tests/unit/onnx/test_fold_casts.py (1)
86-97: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueRedundant dtype plumbing in
_initializer_q_model.
input_dtypeandoutput_dtypeare computed identically, andoutputs[0]is built withFLOAT16at Line 86 only to be overwritten at Line 97. Collapse into a single variable used at construction time.♻️ Proposed simplification
- outputs = [helper.make_tensor_value_info("y", TensorProto.FLOAT16, [None, 4])] + float_dtype = TensorProto.FLOAT16 if opset >= 19 else TensorProto.FLOAT + outputs = [helper.make_tensor_value_info("y", float_dtype, [None, 4])] if shared: nodes.append(helper.make_node("Identity", ["w"], ["w_out"], "identity")) outputs.append(helper.make_tensor_value_info("w_out", TensorProto.FLOAT, [4, 4])) @@ - input_dtype = TensorProto.FLOAT16 if opset >= 19 else TensorProto.FLOAT - output_dtype = TensorProto.FLOAT16 if opset >= 19 else TensorProto.FLOAT - outputs[0].type.tensor_type.elem_type = output_dtype return helper.make_model( helper.make_graph( nodes, "g", - [helper.make_tensor_value_info("x", input_dtype, [None, 4])], + [helper.make_tensor_value_info("x", float_dtype, [None, 4])],🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@tests/unit/onnx/test_fold_casts.py` around lines 86 - 97, In `_initializer_q_model`, replace the redundant `input_dtype` and `output_dtype` calculations with one shared dtype variable derived from `opset`, and use it when constructing the primary `y` output instead of creating it as `FLOAT16` and mutating it afterward. Remove the subsequent `outputs[0].type.tensor_type.elem_type` assignment while preserving the existing opset-dependent dtype.examples/torch_onnx/torch_quant_to_onnx.py (1)
232-259: 🎯 Functional Correctness | 🔵 Trivial | 💤 Low value
_calibrate_new_quantizersmutates_if_quantstate without honoring pre-existing disabled-quant quantizers.
enabled_quantizersis filtered byis_enabled(i.e._disabled), not by_if_quant. Any quantizer that was enabled but intentionally haddisable_quant()applied earlier getsenable_quant()in thefinallyblock, silently turning quantization back on. Filtering onmodule._if_quantinstead would make the save/restore symmetric.♻️ Suggested tweak
- enabled_quantizers = [ - module - for module in model.modules() - if isinstance(module, TensorQuantizer) and module.is_enabled - ] + enabled_quantizers = [ + module + for module in model.modules() + if isinstance(module, TensorQuantizer) and module.is_enabled and module._if_quant + ]🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@examples/torch_onnx/torch_quant_to_onnx.py` around lines 232 - 259, Update _calibrate_new_quantizers to track quantizers based on their pre-existing _if_quant state rather than is_enabled when building enabled_quantizers. Restore quantization only for quantizers whose _if_quant state was originally active, preserving intentionally disabled quantizers through the finally block.tests/examples/torch_onnx/test_torch_quant_to_onnx.py (2)
35-62: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winUnguarded
producers[...]lookups will raiseKeyErrorinstead of a useful failure.If an
Addinput is a graph input or an initializer (no producer node), Line 54/56 raisesKeyErrorrather than an assertion explaining what the graph looks like. Same for thenext(...)lookups at Lines 88 and 95, which raiseStopIterationif the exporter ever emitsMatMul/ReduceMeaninstead ofGemm/GlobalAveragePool. Preferproducers.get(...)plus explicit asserts so failures are diagnosable.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@tests/examples/torch_onnx/test_torch_quant_to_onnx.py` around lines 35 - 62, Harden _assert_residual_adds_are_quantized against missing graph producers by replacing direct producers[...] accesses with producers.get(...) and explicit assertions that identify the missing input or node. Apply the same pattern to the MatMul/ReduceMean lookup paths around the next(...) calls, asserting the expected producer exists before dereferencing it while preserving the current validation behavior.
64-116: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winHard-coded node counts (54/52, 53, 12/4/4) need a comment explaining their derivation.
These numbers encode the exact ResNet-50 Q/DQ topology, but nothing in the test says where they come from, so a future exporter change produces an unexplainable
assert 53 == 52. A one-line comment per assertion (e.g. "53 = 53 Conv weights, fc weight quantizer disabled") would make the failures actionable. As per path instructions, checked-in tests should "document expected behavior".🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@tests/examples/torch_onnx/test_torch_quant_to_onnx.py` around lines 64 - 116, Add concise comments immediately before each hard-coded topology-count assertion in the quantization test, explaining how the expected values derive from the ResNet-50 Q/DQ structure and mode-specific behavior. Cover the activation quantizer counts, DQ fanout counts, and int8 weight quantizer count, including details such as the number of convolution weights and disabled fully connected weight quantization; leave the assertions unchanged.Source: Path instructions
modelopt/torch/_deploy/utils/torch_onnx.py (1)
48-48: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueImporting a private helper across modules.
_convert_q_data_initializers_to_fp16is underscore-private tomodelopt/onnx/utils.pybut is consumed here. Since it's now part of the export pipeline contract, consider promoting it to a public name (and adding it to that module's__all__) so the dependency is explicit.Note the sequencing is load-bearing: this call raises if any Q scale is still FP32 after Line 670, which is why the stricter fold guard in
modelopt/onnx/utils.pymatters (flagged there).Also applies to: 669-673
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@modelopt/torch/_deploy/utils/torch_onnx.py` at line 48, Promote _convert_q_data_initializers_to_fp16 in modelopt/onnx/utils.py to a public helper name, add that name to the module’s __all__, and update the import and call sites in the ONNX export flow around _convert_q_data_initializers_to_fp16 accordingly. Preserve the existing sequencing and validation behavior after the fold guard.Source: Coding guidelines
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@examples/torch_onnx/torch_quant_to_onnx.py`:
- Around line 363-392: Update _finalize_resnet_quantizers so the int8/fp8 path
only accesses quantizer attributes when they exist, using safe getattr checks
for block.conv1, model.fc, model.global_pool, and related quantizers. Preserve
enabling/disabling behavior for present quantizers, and avoid AttributeError
when _prepare_resnet_quantizers skipped setup or a block variant lacks these
attributes.
In `@modelopt/onnx/export/fp8_exporter.py`:
- Line 34: Update the weight-channel threshold logic near the FP8
minimum-channel constant and the relevant convolution export check to account
for the convolution’s group count, matching the torch-side gate. Base the
decision on the effective total input channels rather than per-group
weight_input.values.shape[1], so grouped and depthwise convolutions consistently
enable or skip activation and weight Q/DQ paths.
In `@modelopt/onnx/utils.py`:
- Around line 1471-1481: The Cast-folding condition in the loop over
onnx_model.graph.node should not reject Cast-to-FLOAT nodes solely because
tensor_types lacks node.input[0]. Treat missing input type metadata as eligible
for folding, or resolve the producer output type before skipping; preserve the
existing FLOAT16 exclusion when the type is explicitly known.
- Around line 1524-1534: Replace the hard ValueError in the Q-consumer
validation with graceful skipping of that initializer, and emit a warning
identifying q_node.name and the observed scale dtype. Ensure
get_onnx_bytes_and_metadata continues exporting when a Q scale is a graph input
or remains non-FP16, while preserving FP16 conversion for valid Q scales.
---
Nitpick comments:
In `@examples/torch_onnx/torch_quant_to_onnx.py`:
- Around line 232-259: Update _calibrate_new_quantizers to track quantizers
based on their pre-existing _if_quant state rather than is_enabled when building
enabled_quantizers. Restore quantization only for quantizers whose _if_quant
state was originally active, preserving intentionally disabled quantizers
through the finally block.
In `@modelopt/torch/_deploy/utils/torch_onnx.py`:
- Line 48: Promote _convert_q_data_initializers_to_fp16 in
modelopt/onnx/utils.py to a public helper name, add that name to the module’s
__all__, and update the import and call sites in the ONNX export flow around
_convert_q_data_initializers_to_fp16 accordingly. Preserve the existing
sequencing and validation behavior after the fold guard.
In `@tests/examples/torch_onnx/test_torch_quant_to_onnx.py`:
- Around line 35-62: Harden _assert_residual_adds_are_quantized against missing
graph producers by replacing direct producers[...] accesses with
producers.get(...) and explicit assertions that identify the missing input or
node. Apply the same pattern to the MatMul/ReduceMean lookup paths around the
next(...) calls, asserting the expected producer exists before dereferencing it
while preserving the current validation behavior.
- Around line 64-116: Add concise comments immediately before each hard-coded
topology-count assertion in the quantization test, explaining how the expected
values derive from the ResNet-50 Q/DQ structure and mode-specific behavior.
Cover the activation quantizer counts, DQ fanout counts, and int8 weight
quantizer count, including details such as the number of convolution weights and
disabled fully connected weight quantization; leave the assertions unchanged.
In `@tests/unit/onnx/test_fold_casts.py`:
- Around line 86-97: In `_initializer_q_model`, replace the redundant
`input_dtype` and `output_dtype` calculations with one shared dtype variable
derived from `opset`, and use it when constructing the primary `y` output
instead of creating it as `FLOAT16` and mutating it afterward. Remove the
subsequent `outputs[0].type.tensor_type.elem_type` assignment while preserving
the existing opset-dependent dtype.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Enterprise
Run ID: b4d1c1cd-df7c-44b1-be7d-a25ddb7f9ddb
📒 Files selected for processing (8)
CHANGELOG.rstexamples/torch_onnx/torch_quant_to_onnx.pymodelopt/onnx/export/fp8_exporter.pymodelopt/onnx/utils.pymodelopt/torch/_deploy/utils/torch_onnx.pytests/examples/torch_onnx/test_torch_quant_to_onnx.pytests/unit/onnx/quantization/test_fp8_mha_exporter.pytests/unit/onnx/test_fold_casts.py
🚧 Files skipped from review as they are similar to previous changes (1)
- CHANGELOG.rst
Co-Authored-By: Codex <noreply@openai.com> Signed-off-by: ajrasane <131806219+ajrasane@users.noreply.github.com>
There was a problem hiding this comment.
Warning
CodeRabbit couldn't request changes on this pull request because it doesn't have sufficient GitHub permissions.
Please grant CodeRabbit Pull requests: Read and write permission and re-run the review.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@modelopt/onnx/export/fp8_exporter.py`:
- Around line 196-197: Update the input-stem skip around the weight
dequantization pass so it applies only when the export is identified as a
ResNet, rather than using a three-channel input as the model-family
discriminator. Propagate and check an explicit ResNet/export marker alongside
node.inputs[0].name and weight_input.values.shape, while preserving the skip for
ResNet input stems and normal FP8 DQ restoration for non-ResNet models.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Enterprise
Run ID: 22b23dca-030c-4c44-891d-67056a837b68
📒 Files selected for processing (4)
examples/torch_onnx/torch_quant_to_onnx.pymodelopt/onnx/export/fp8_exporter.pytests/examples/torch_onnx/test_torch_quant_to_onnx.pytests/unit/onnx/quantization/test_fp8_mha_exporter.py
🚧 Files skipped from review as they are similar to previous changes (1)
- tests/examples/torch_onnx/test_torch_quant_to_onnx.py
Co-Authored-By: Codex <noreply@openai.com> Signed-off-by: ajrasane <131806219+ajrasane@users.noreply.github.com>
Co-Authored-By: Codex <noreply@openai.com> Signed-off-by: ajrasane <131806219+ajrasane@users.noreply.github.com>
Co-Authored-By: Codex <noreply@openai.com> Signed-off-by: ajrasane <131806219+ajrasane@users.noreply.github.com>
There was a problem hiding this comment.
Warning
CodeRabbit couldn't request changes on this pull request because it doesn't have sufficient GitHub permissions.
Please grant CodeRabbit Pull requests: Read and write permission and re-run the review.
Actionable comments posted: 1
🧹 Nitpick comments (1)
modelopt/torch/quantization/plugins/timm.py (1)
16-25: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueAdd
__all__for this module's public API.Only
is_resnet_quantization_supportedis a non-underscore (public) symbol here; everything else is intentionally private. Declaring__all__ = ["is_resnet_quantization_supported"]makes that contract explicit and keepsfrom .timm import *inplugins/__init__.pypredictable if more public helpers are added later.As per coding guidelines, "Define each module's public API with
__all__ = [...]."🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@modelopt/torch/quantization/plugins/timm.py` around lines 16 - 25, Add a module-level __all__ declaration in timm.py containing only is_resnet_quantization_supported, preserving the intended public API for wildcard imports.Source: Coding guidelines
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@modelopt_recipes/README.md`:
- Around line 79-84: Update the “Choosing where to look” guidance in README.md
to add a timm-specific lookup step before the general fallback, directing
readers to timm/<architecture>/ for architecture-specific recipes. Preserve the
existing huggingface and general guidance while ensuring timm recipes are
included in the selection flow.
---
Nitpick comments:
In `@modelopt/torch/quantization/plugins/timm.py`:
- Around line 16-25: Add a module-level __all__ declaration in timm.py
containing only is_resnet_quantization_supported, preserving the intended public
API for wildcard imports.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Enterprise
Run ID: 62647b7a-3e4f-40ce-bedb-56b81fdb85f9
📒 Files selected for processing (22)
CHANGELOG.rstexamples/torch_onnx/README.mdexamples/torch_onnx/torch_quant_to_onnx.pymodelopt/torch/opt/dynamic.pymodelopt/torch/quantization/algorithms.pymodelopt/torch/quantization/conversion.pymodelopt/torch/quantization/plugins/__init__.pymodelopt/torch/quantization/plugins/timm.pymodelopt_recipes/README.mdmodelopt_recipes/timm/resnet/ptq/README.mdmodelopt_recipes/timm/resnet/ptq/fp8.yamlmodelopt_recipes/timm/resnet/ptq/int8.yamlmodelopt_recipes/timm/resnet/ptq/mxfp8.yamlmodelopt_recipes/timm/resnet/ptq/nvfp4.yamlmodelopt_recipes/timm/resnet/ptq/nvfp4_awq_lite.yamlmodelopt_recipes/timm/resnet/ptq/static_fp8.quant_cfg.yamlmodelopt_recipes/timm/resnet/ptq/static_int8.quant_cfg.yamltests/unit/torch/nas/test_registry.pytests/unit/torch/quantization/plugins/test_timm.pytests/unit/torch/quantization/test_autoquant.pytests/unit/torch/quantization/test_config_validation.pytests/unit/torch/quantization/test_quantize_cpu.py
🚧 Files skipped from review as they are similar to previous changes (1)
- examples/torch_onnx/README.md
Keep the change focused on shortcut QDQ placement with FP8, INT8, and AutoQuantize recipes. Co-Authored-By: Codex <noreply@openai.com> Signed-off-by: ajrasane <131806219+ajrasane@users.noreply.github.com>
cjluo-nv
left a comment
There was a problem hiding this comment.
Bot review (bedrock-claude-opus-5) — DM the bot to share feedback.
Re-review of the reworked (recipe-driven) implementation. The approach changed substantially since the last round, and most of the earlier findings are genuinely resolved:
Resolved
- Duplicated calibration / second data pass (critical): the manual
enable_calib→ forward-loop →load_calib_amaxblock is gone._add_resnet_residual_quantizersnow runs as aCUSTOM_POST_CONVERSION_PLUGINScallback, i.e. after_replace_quant_modulebut beforeset_quantizer_by_cfg(seemodelopt/torch/quantization/conversion.py::replace_quant_module), so the recipe's'*residual_quantizer'entry configures it andmtq.quantize's own forward loop calibrates it. The quantizers are also visible toprint_quant_summaryand modelopt state. Good fix. - Strict
load_calib_amax()(critical): moot — standard calibration path is used now. - Auto-mode residual format inferred from the search space (critical): replaced by an explicit design — the residual quantizer is pinned to FP8 via
fixed_quantization_configin the AutoQuantize recipe, documented in the README and PR body. Acceptable as an intentional choice (see one follow-up below). - Test asserted "every
Addis residual" (minor): the test now selects residual adds structurally by their singleReluconsumer before the count assertion.
Still open / new
_disable_dead_quantizersstill only inspectsinput_quantizer/output_quantizer/weight_quantizer, so aresidual_quantizerthat calibrates toamax == 0/NaN reaches the FP8 exporter'sscale = 448 / amax— the exact case that guard was added for. The explicit NaN/non-positive check you described in the previous round no longer exists anywhere in the new code, so this half of the earlier comment regressed (one-line fix).tests/examples/torch_onnx/test_torch_quant_to_onnx.pyruns resnet50 +autowith--trt_build, but the PR body says the AutoQuantize FP8/INT8 mix only builds on Blackwell+. Please confirm how this test is expected to pass on the CI GPUs.- The recipe →
mtq.auto_quantizekwargs translation duplicatesexamples/hf_ptq/hf_ptq.py::_mtq_inputs_from_auto_quantize_config, and the copy silently dropsscore_size(the new recipe setsscore_size: 128but the example uses--num_score_steps),cost_excluded_layers, and top-levelcandidate_formats. --auto_quantization_formatsis silently overridden for ResNet inautomode with no warning and no opt-out (--recipeis rejected with--quantize_mode=auto).- Nits: the two new helpers are the only ones in this module without docstrings (the rationale docstring added in the previous round didn't survive the rewrite);
"residual_quantizer" in block.downsample._modulescould usehasattr;modelopt_recipes/README.mdstill doesn't mention the new top-leveltimm/tree (earlier bot comment unaddressed);timm/resnet/ptq/{fp8,int8}.yamlare verbatim copies ofconfigs/ptq/presets/model/{fp8,int8}.yamlplus one entry, so they will drift if the presets change.
No licensing concerns (new YAMLs carry the standard NVIDIA SPDX header); no prompt-injection content in the PR metadata.
Resolve the torch ONNX example conflicts while preserving the residual-only recipe scope. Co-Authored-By: Codex <noreply@openai.com> Signed-off-by: ajrasane <131806219+ajrasane@users.noreply.github.com>
Co-Authored-By: Codex <noreply@openai.com> Signed-off-by: ajrasane <131806219+ajrasane@users.noreply.github.com>
b0b337f to
0a565bd
Compare
cjluo-nv
left a comment
There was a problem hiding this comment.
Bot review (bedrock-claude-opus-5) — DM the bot to share feedback.
All previously-flagged critical items are resolved (residual quantizers now go through the standard calibration flow via CUSTOM_POST_CONVERSION_PLUGINS, _disable_dead_quantizers covers residual_quantizer, residual adds are selected structurally in the test, --recipe is authoritative instead of silently overriding flags, and the AutoQuantize mapping no longer drops score_size/cost_excluded_layers/candidate_formats). Design-wise this reuses the existing recipe/preset/plugin machinery rather than inventing a new one — good.
Three things I'd like addressed before merge:
-
_match_qformatcan never match a recipe-derived config, so every recipe path silently skips the TRT post-processing.QUANT_CFG_CHOICESentries aremodel_dump(exclude_unset=True)(sparse:{"quantizer_name": "*", "enable": false}), while_match_qformatcompares them againstrecipe.quantize.model_dump()(dense:parent_class: None,cfg: None, plus ~20QuantizerAttributeConfigdefaults).entry in config["quant_cfg"]is therefore always False, so_recipe_qformats()returns an empty set and_prepare_auto_quantize_format()never adds the Conv2d override. Concretely:--recipe=timm/resnet/ptq/fp8never runs_disable_low_channel_conv_input_quantizers, i.e. the raw-RGBconv1keeps its FP8 input quantizer — the exact Blackwell "no implementation forTRT_FP8QuantizeLinear" failure the helper exists to prevent. CI on Ada hides this. Same class of bug for recipe-suppliedmxfp8/nvfp4AutoQuantize candidates (no Conv2d→FP8 override, and_disable_high_rank_input_quantizersskipped). -
ResNet +
autois silently dropped from a previously-supported/tested configuration (README ✅ → blank, testpytest.skip("AutoQuantize is not supported for ResNet")). The flag-based auto path for resnet50 with--trt_buildwas passing before this PR; if the reason is the now-unconditional residual-quantizer registration, register the plugin only when residual quantization is actually requested, and otherwise please explain the regression in the PR body/CHANGELOG. -
New recipe YAMLs carry a truncated license header (2-line SPDX only) whereas every other
modelopt_recipes/**.yamluses the fullLICENSE_HEADERApache text. Not auto-enforced (theinsert-licensehook only covers python/shell/c), so it needs a manual fix.
Also: there is no unit coverage for _match_qformat / _recipe_qformats / _mtq_inputs_from_auto_quantize_config — a small pure-Python test asserting that the new timm/resnet/ptq/fp8.yaml resolves to {"fp8"} would have caught (1) immediately.
|
Thank you @ajrasane. Can you please add the runtime perf numbers for the Torch and ONNX quantization paths to confirm parity? Thanks! |
0a565bd to
bfe06d9
Compare
Co-Authored-By: Codex <noreply@openai.com> Signed-off-by: ajrasane <131806219+ajrasane@users.noreply.github.com>
bfe06d9 to
291925d
Compare
Benchmarked on an NVIDIA RTX 6000 Ada Generation GPU with TensorRT 10.16.0.72. Disclaimer: These are rough estimates and should not be considered official performance numbers, as the measurements may include some overheads.
|
Co-Authored-By: Codex <noreply@openai.com> Signed-off-by: ajrasane <131806219+ajrasane@users.noreply.github.com>
cjluo-nv
left a comment
There was a problem hiding this comment.
Bot review (bedrock-claude-opus-5) — DM the bot to share feedback.
Re-review (8 files, +448/-144). Design-review protocol applied first: this PR does not introduce a new subsystem — the residual quantizer is installed through the existing CUSTOM_POST_CONVERSION_PLUGINS hook (invoked by replace_quant_module before set_quantizer_by_cfg, so the recipe configures it and mtq.quantize's own forward loop calibrates it), and the new YAMLs slot into the existing modelopt_recipes/ catalog with $import units. That's reuse of the in-repo pattern, not a second one. Licensing is fine: the two new recipe YAMLs now carry the verbatim LICENSE_HEADER text. No prompt-injection content in the PR metadata.
Previous blockers — resolved
_match_qformatcould never match a recipe-derived config, so--recipesilently skipped TRT post-processing: the reverse-preset-matching helpers are gone._disable_low_channel_fp8_conv_input_quantizersand_disable_high_rank_input_quantizersnow run unconditionally and decide from live quantizer state (num_bits == (4, 3),block_sizes), so--recipe=timm/resnet/ptq/fp8does disable the raw-RGBconv1input quantizer. Confirmed the loader convertseXmY→ tuples, so the(4,3)/(2,1)comparisons are sound.- Truncated SPDX-only header on the new YAMLs: fixed, matches
LICENSE_HEADER. modelopt_recipes/README.mddidn't mention thetimm/tree: fixed (catalog + selection guidance + placement), plus atimm/resnet/ptq/README.mddocumenting the delta.- AutoQuantize mapping dropped
score_size/cost_excluded_layers/candidate_formats: all three are mapped now, andtest_auto_quantize_recipe_mapping+ a new e2e recipe test cover the top-level-candidates shape (previously themodule_search_spaces=[]failure case). - Residual plugin registered unconditionally: now gated on the recipe actually enabling
*residual_quantizer, and_disable_dead_quantizerscoversresidual_quantizer.
Also verified the removal of _calibrate_uncalibrated_quantizers is safe: forcing algorithm = "max" for mxfp8/nvfp4 calibrates the FP8 Conv overrides in the normal pass, and finish_stats_collection skips amax loading for dynamic/MX quantizers.
Why a nudge rather than an approve — see the reason field: the remaining items are product/behavior judgment calls plus a few small robustness gaps, not a re-flag of the fixed bugs.
Thanks @ajrasane , can you please also add the before and after this fix numbers so we can show the need of this PR? Thanks! |
| if uses_fp8_conv_input: | ||
| _disable_low_channel_conv_input_quantizers(quantized_model) | ||
| if is_resnet: | ||
| _validate_resnet_quantizers(quantized_model) |
There was a problem hiding this comment.
Is this only needed for ResNet or any models that require QDQ nodes to be placed in the residual branch?
There was a problem hiding this comment.
Currently I have validated this only for the ResNet models. Are there any other models with Residual connections we would like to target? I can try to find a more general solution using them.
|
Is there a way to make this behavior generic to other models with residual connections? Per my understanding, this logic would require each relevant model to have yaml files in |
I can try to make this behavior generic for a few Convolutional models we would like to support. But I think it will be hard to guarantee that this would be applicable for all models with Residual connections as well. To do this, we will need to implement a graph parsing logic at the torch level to check for the residual connection pattern which is out of scope for the current PR.
Yes, the current presets are too generic. Users can get runnable quantized models with them, but they are not guaranteed to be the most performant. So it would be better to have per model configs that provide the best performance. Users can tweak their custom models by using these configs as guidance. This will also simplify a lot of code on the export and post-processing side where we inject/remove quantizers to give the best performance. |
Co-Authored-By: Codex <noreply@openai.com> Signed-off-by: ajrasane <131806219+ajrasane@users.noreply.github.com>
Disclaimer: These are rough estimates and should not be considered official performance numbers, as the measurements may include some overheads.
|
Understood, is that something that we can try to add in a follow-up PR? Here are some models that have residual connections that might be helpful to validate the more generic workflow:
Thanks! |
Thank you! This is very clear in showing the benefits of this PR and parity with ONNX quant for INT8 while keeping FP8 performance. |
gcunhase
left a comment
There was a problem hiding this comment.
LGTM, let's move efforts to make this a more generic workflow to a follow-up PR. Thanks!
What does this PR do?
Type of change: Bug fix
Adds recipe-backed FP8 and INT8 residual quantization for timm ResNet models in the torch ONNX example:
Add.--recipesupport for PTQ and AutoQuantize recipes and renames--quantize_modeto--qformat.Add.ResNet support scope
ResNet and other convolutional architectures are supported only with FP8 and INT8. AutoQuantize, MXFP8, NVFP4, and INT4_AWQ are not supported for ResNet because TensorRT has limited convolution kernel support. Transformer architectures containing individual Conv2d layers continue to use format-specific Conv overrides.
Usage
python examples/torch_onnx/torch_quant_to_onnx.py \ --timm_model_name=resnet50 \ --recipe=timm/resnet/ptq/fp8 \ --onnx_save_path=resnet50.onnxUse
timm/resnet/ptq/int8for INT8. Without--recipe,--qformatselects a built-in quantization preset.Testing
Before your PR is "Ready for review"
CONTRIBUTING.md: N/A